package com.yrd.fssync.plugin;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPReply;

import com.yrd.fssync.core.ISyncer;
import com.yrd.fssync.core.SyncRequest;

public class FileSyncer implements ISyncer {
	String remoteBase;
	
	public FileSyncer(String remoteBase) {
		this.remoteBase = remoteBase;
		new File(remoteBase).mkdirs();
	}

	public void open() throws SocketException, IOException{
	}
	
	public void dispose(SyncRequest request) throws IOException{
			String remotePath = remoteBase+"/"+StringUtils.defaultString(request.getRemote());
			File remoteFile = new File(remotePath);
			if(request.isDir()){
				if(request.isOverwrite()){
					remoteFile.mkdirs();
				}else if(request.isDelete()){
					FileUtils.deleteQuietly(remoteFile);
				}
			}else if(request.isFile()){
				if(request.isOverwrite()){
					remoteFile.getParentFile().mkdirs();
					FileUtils.copyFile(request.getLocalFile(), remoteFile);
				}else if(request.isDelete()){
					FileUtils.deleteQuietly(remoteFile);
				}
			}
	}
	
	public void close(){
	}

	@Override
	public String toString() {
		return "FileSyncer [remoteBase=" + remoteBase + "]";
	}


	
	
}
